<template>
    
    <!-- 组件的模板 -->
    <div class="page signin-manage">
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell weui-cell_switch">
                <div class="weui-cell__bd">自动清理</div>
                <div class="weui-cell__ft">
                    <input class="weui-switch" type="checkbox" v-model="clean_switch" :true-value="1" :false-value="0">
                </div>
            </div>
        </div>
        <div class="weui-cells__tip">开启后，自动清理掉项目中一定天数未工作的人员</div>
        <div class="weui-cells">
            <template v-if="clean_switch == 1">
                <a class="weui-cell weui-cell_access" href="javascript:;" @click="goXmmerberClean(this.clean_cycle)">
                    <div class="weui-cell__bd">
                        <p>清理周期</p>
                    </div>
                    <div class="weui-cell__ft">
                        <span>{{clean_cycle}}天</span>
                    </div>
                </a>  
            </template>
        </div>
    </div>   
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import widget from 'gapi/widget'
    import clientUi from 'gapi/clientUi'
    import * as util from 'js/util.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                team_id: 0,
                project_id: 0,
                clean_switch: false,
                clean_cycle: '',
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                
                this.project_id = util.getUrddataString('projectId')
                this.team_id = util.getUrddataString('teamId')
                this.initAjax();
                
                
            },
            initAjax (){
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url: '/project/auto_clean/get',
                    type:'POST',
                    data: {
                            team_id: this.team_id,
                            project_id: this.project_id
                        },
                    success: (res) => {
                        console.log(res)
                        commonPage.loadPageStatus('loadSuccess')
                        if(res&&res.errno==0){
                            this.clean_switch = res.data.auto_clean_no
                            this.clean_cycle = res.data.clean_cycle
                            this.doWatch()
                        }else if(res.errmsg){
                             widget.toast(res.errmsg)
                        }else{
                            widget.toast('数据获取失败')
                        }
                    },
                    error: (xhr, error) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络,设置失败')
                    }
                })
            },
            submit (){
                let ajaxData = {
                        team_id: this.team_id,
                        project_id: this.project_id,
                        auto_clean_no: this.clean_switch,
                        clean_cycle: this.clean_cycle
                    }
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url: '/project/auto_clean/update',
                    type:'POST',
                    data: ajaxData,
                    success: (res) => {
                        commonPage.loadPageStatus('loadSuccess')
                        if(res&&res.errno==0){                            
                            widget.toast('设置成功')
                        }else if(res.errmsg){
                             widget.toast(res.errmsg)
                        }else{
                            widget.toast('数据获取失败')
                        }
                    },
                    error: (xhr, error) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络,设置失败')
                    }
                })
            },
            //更新清理时间
            updateData (data){
                if(!data)return//兼容ios无操作返回时会触发loadData  并返回 0
                if(data.key&&data.key=='goXmmerberClean'){
                    this.clean_cycle = data.value.id
                }                
            },
            goXmmerberClean (clean_cycle){
                commonPage.getWebViewID((data)=>{
                  cache.setMemoryCache('webViewID',{'webViewID':data})
                })
                location.href = "dmsaas://XmmerberClean?urdData=" + encodeURIComponent(`tId=${this.team_id}&pId=${this.project_id}&clean_cycle=${clean_cycle}`)
            },
            doWatch(){
                this.$watch('clean_switch', (newVal, oldVal) => {
                    if(newVal != oldVal){
                        this.submit()
                    }
                })
                this.$watch('clean_cycle', (newVal, oldVal) =>{
                    if(newVal != oldVal){
                        this.submit()
                    }
                })
            }
        },
        created(){

            this.init();
            
            window.loadData = this.updateData
            
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<!-- <style src="static/css/global.css"></style> -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/attendance.css"></style>



